(function(){

	nsbase.views.mapGame = new  MapGameFunc();

	function MapGameFunc() {
		var _self = this;

		_self.init = init;

		var $mapContent = $(".mapContent"),
			curMap,
			$year = $("#year"),
			$month = $("#month")
			$day = $("#day");

		var gameUrls = nsbase.config.actionUrl.game,
			utilities = nsbase.utility;
		function init () {
      		$(".nav_menu li.Online").addClass("selected");
			bindHandlers();
			initDateSelecter();
			slideMapList();
		}

		function slideMapList () {
			var mapId = parseInt($("#mapId").val());
			$mapContent.find("li[map-id='" + mapId + "']").addClass("visible");
			$mapContent.arrowSlide({sliderChangedHandler:syncMapName});
		}

		function bindHandlers() {
			$("#btnCreate").click(function(){
				var sender = $(this),
					container = $(this).parents(".g_info:first");
				container.mask();
				var mapModel = getMapModel();
				var params ={
					map_id:mapModel.id,
					openness: nsbase.enums.gameOpennessEnum.privacy.value,// 1 : 公共 2 : 好友，非邀请好友不可见 3 : 好友，非邀请好友可见 4 : 群组 5 : 个人
					ticket:0,
					start_date: $year.val() + $month.val()+ $day.val(),
					invite_list:null
				};
				nsbase.ajax.request(gameUrls.createMapGame,params,function(result){
					container.unmask();
					if(result.success){
						location.href = nsbase.config.actionUrl.game.mapGameDetail + "?game_id=" + result.data;
					} else {
						utilities.errorDialog(result.message)
					}
				},function(){
					container.unmask();
				});
			});
		}

		function getMapModel () {
			var mapModel = {
				id:"",
				name:"",
				length:0
			};
			var $mapObj = $mapContent.find("li.visible").filter(":not(.cloned)");
			if($mapObj.length == 0){
				$mapObj = $mapContent.find("li:first");
			}
			mapModel.id = $mapObj.attr("map-id");
			mapModel.name = $mapObj.find(".map_name").text();
			mapModel.length = $mapObj.attr("map-length");
			return mapModel;
		}

		function syncMapName(){
			var mapModel = getMapModel();
			$(".gameName").text(mapModel.name);
			$("#mapLength").text(mapModel.length);
		}

		function initDateSelecter () {
			var date = new Date(),
				year = date.getFullYear();
				startMonth = date.getMonth() + 1,
				startDate = date.getDate();

			
			resetYear();
			resetMonth(startMonth);
			resetDate(startDate);

			$year.change(function(){
				var curYear = parseInt($year.val());
				var beginMonth = 1;
				$month.empty();
				if(curYear === year){
					beginMonth = startMonth;
				}
				resetMonth(beginMonth);
				resetDate(1);
			});
			$month.change(function(){
				var curMonth = parseInt($(this).val());
				var curYear = parseInt($year.val())
				var monthDates = nsbase.utility.getMonthDates(curYear,curMonth-1);
				$day.empty();

				var beginDate = 1;
				if(curMonth == startMonth){
					beginDate = startDate;
				}
				resetDate(beginDate);
			});

			function resetYear(){
				for (var i = 0; i < 2; i++) {
					var yearValue = year + i;
					$year.append('<option value="' + yearValue + '">'+yearValue+'</option>');
				}
				$year.val(year);
			}
			function resetMonth(beginMonth){
				for (var i = beginMonth; i <= 12; i++) {
					var monthValue = i + "";
					if(i<10){
						monthValue = "0"+ i;
					}
					$month.append('<option value="' + monthValue + '">' + i + '</option>');
				}
				$month.val(beginMonth>9?beginMonth:"0"+beginMonth);
			}
			function resetDate(beginDate){
				var curMonth = parseInt($month.val());
				var curYear = parseInt($year.val())
				var monthDates = nsbase.utility.getMonthDates(curYear,curMonth-1);

				for (var i = beginDate; i <= monthDates; i++) {
					var dateValue = i + "";
					if(i < 10){
						dateValue = "0"+ i;
					}
					$day.append('<option value="'+ dateValue +'">'+i+'</option>');
				}
				$day.val(beginDate>9?beginDate:"0"+beginDate);
			}
		}

		
	}
	
})();